package com.youlu.campus.web.community.controller;


import com.youlu.campus.entity.VO.req.QaCommitCommand;
import com.youlu.campus.entity.VO.req.QaInfoReq;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.qa.ActivityQaInfoService;
import com.youlu.campus.service.qa.UserQaInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class ActivityUserQaController {

    @Autowired
    private UserQaInfoService userQaInfoService;

    @Autowired
    private ActivityQaInfoService activityQaInfoService;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;


    /**
     * 查询用参与活动
     * @param
     * @return
     */
    @GetMapping("/mini-app/user-join/activity")
    public QResult userJoinActivity(){
        return QResult.success(userParticipateActivityInfoService.findJoinActivityByUserId(RequestContext.getUserId()));
    }

    /**
     * 根据活动ID查询用户答题
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user-qa/list")
    public QResult userQaList(@RequestParam("activityId") String activityId ){
        return QResult.success(userQaInfoService.findByActivityId(RequestContext.getCurUser(),activityId));
    }

    /**
     * 查询获取答题信息
     * @param qaInfoReq
     * @return
     */
    @PostMapping("/mini-app/user-qa/query")
    public QResult qaInfo(@RequestBody @Validated QaInfoReq qaInfoReq,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        qaInfoReq.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(activityQaInfoService.getQaInfo(qaInfoReq));
    }


    /**
     * 答题提交
     * @param
     * @return
     */
    @PostMapping("/mini-app/user-qa/commit")
    public QResult qaCommit(@RequestBody @Validated QaCommitCommand commitCommand, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userQaInfoService.doQaCommit(commitCommand));
    }

    /**
     * 检测QA答题
     * @param qaInfoReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/user-qa/check")
    public QResult checkQa(@RequestBody @Validated QaInfoReq qaInfoReq,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        userQaInfoService.checkQaLimit(1, RequestContext.getUserId(),qaInfoReq.getQaInfoId());
        return QResult.success(null);
    }



}
